package org.bozo.im.service.protocols.xmpp.stanza.processor;

import java.io.IOException;

import org.bozo.im.service.Constants;
import org.bozo.im.service.protocols.xmpp.XMPPSession;
import org.bozo.im.service.protocols.xmpp.stanza.Stanza;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Log;

public abstract class StanzaProcessor {

	public abstract Stanza getStanza();

	public void processStanza(XmlPullParser pParser, XMPPSession pSession, StanzaProcessor pParentProcessor) throws XmlPullParserException, IOException {

		// ---------------------------------------------------------------------------------------
		Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.xmpp.stanza.processor.StanzaProcessor.processStanza ---------- IN");
		// ---------------------------------------------------------------------------------------

		String lCurrentName = getStanza().getName();

		pParser.next();

		while (!(pParser.getEventType() == XmlPullParser.END_TAG && pParser.getName().equals(lCurrentName))) {

			if (pParser.getEventType() == XmlPullParser.START_TAG) {

				Stanza lStanza = Stanza.valueOf(pParser.getName(), pParser.getNamespace());

				// ---------------------------------------------------------------------------------------
				Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.xmpp.stanza.processor.StanzaProcessor.processStanza ---------- Parsing XML node: " + lStanza);
				// ---------------------------------------------------------------------------------------

				StanzaProcessor lStanzaProcessor = pSession.getStanza(lStanza);

				if (lStanzaProcessor != null) {
					lStanzaProcessor.processStanza(pParser, pSession, this);
				} else {
					// ---------------------------------------------------------------------------------------
					Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.xmpp.stanza.processor.StanzaProcessor.processStanza ---------- Unknown stanza : " + pParser.getName());
					// ---------------------------------------------------------------------------------------
				}
			}
			
			pParser.next();
		}

		// ---------------------------------------------------------------------------------------
		Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.xmpp.stanza.processor.StanzaProcessor.processStanza ---------- OUT");
		// ---------------------------------------------------------------------------------------
	}

	@Override
	public final String toString() {
		return getStanza().getName();
	}
}
